package bean;

import lombok.Getter;
import lombok.Setter;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Employee
 * HADOOP的序列化类
 *
 * @author zhangyimin
 * @version 1.0
 * @date 2018-07-31 上午10:08
 */
@Getter
@Setter
public class Employee implements WritableComparable<Employee> {

    /**
     * 员工号
     */
    private int empNo;

    /**
     * 员工姓名
     */
    private String name;

    /**
     * 薪资
     */
    private int sal;

    /**
     * 部门号
     */
    private int deptNo;


    @Override
    public int compareTo(Employee o) {
        //多列排序   SELECT * FROM EMP A ORDER BY A.dept_no,A.sal;
        if (this.deptNo> o.getDeptNo()){
            return 1;
        }else if (this.deptNo< o.getDeptNo()){
            return -1;
        }else if (this.sal>o.getSal()){
            return 1;
        }else {
            return -1;
        }

        //单列排序   SELECT * FROM EMP A ORDER BY A.sal;
//        if (this.sal>o.getSal()){
//            return 1;
//        }else {
//            return -1;
//        }




    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(this.empNo);
        dataOutput.writeUTF(this.name);
        dataOutput.writeInt(this.sal);
        dataOutput.writeInt(this.deptNo);

    }                                                                             

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.empNo = dataInput.readInt();                                        
        this.name = dataInput.readUTF();
        this.sal = dataInput.readInt();
        this.deptNo = dataInput.readInt();
    }

    @Override
    public String toString() {
        return "Employee{" +
                "empNo=" + empNo +
                ", name='" + name + '\'' +
                ", sal=" + sal +
                ", deptNo=" + deptNo +
                '}';
    }
}
